﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.Web;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using MediaCoders.NopCommerce.Code.MultiSafePay;
using NopSolutions.NopCommerce.BusinessLogic.Audit;
using NopSolutions.NopCommerce.BusinessLogic.Payment;

namespace MediaCoders.NopCommerce.PaymentMethods
{
    /// <summary>
    /// Page to handle events from MultiSafePay
    /// </summary>
    public partial class MultiSafePayReturn : BaseNopPage
    {
        #region EventHandler

        protected void Page_Load(object sender, EventArgs e)
        {
            int notification;
            int orderid;
            if (Request.QueryString["s"] != null && Int32.TryParse(Request.QueryString["s"], out notification) &&
                Request.QueryString["o"] != null && Int32.TryParse(Request.QueryString["o"], out orderid))
            {
                Order o = OrderManager.GetOrderById(orderid);
                if (o != null)
                {
                    switch (notification)
                    {
                        //Customer cancelled the transaction
                        case 1:
                            {
                                CancelOrder(orderid, o);
                                Response.Redirect(CommonHelper.GetStoreLocation());
                                break;
                            }
                        //Status update by MultiSafePay
                        case 2:
                            {
                                MultiSafePay msp = new MultiSafePay(o);
                                UpdatePaymentStatus(o ,msp);
                                lit.Text = "<a href=\"" + msp.Merchant.ReturnUrl + "\">" + "Terug naar de webwinkel." + "</a>";
                                break;
                            }
                    }
                }
            }            
        }

        #endregion

        #region Private Methods

        private void CancelOrder(int orderid, Order o)
        {
            if (MultiSafePayPaymentSettings.CancelOrderOnPaymentCancel)
            {
                if (OrderManager.CanCancelOrder(o))
                {
                    OrderManager.CancelOrder(orderid, false);
                }
            }
        }

        private void UpdatePaymentStatus(Order o, MultiSafePay msp)
        {
            if (MultiSafePayPaymentSettings.AutoStatusProcess)
            {
                msp.Transaction.ID = Request.QueryString["transactionid"];
                
                string status = msp.GetStatus();

                LogManager.InsertLog(LogTypeEnum.Unknown, "UpdatePaymentStatus", status);

                if (!string.IsNullOrEmpty(status))
                {
                    if (status.ToUpper() == "COMPLETED")
                    {
                        OrderManager.InsertOrderNote(o.OrderId, "MultiSafePay status: " + status, false, DateTime.Now);
                        if (OrderManager.CanMarkOrderAsPaid(o))
                        {
                            OrderManager.MarkOrderAsPaid(o.OrderId);
                        }
                    }
                }
                else
                {
                    LogManager.InsertLog(LogTypeEnum.OrderError, "UpdatePaymentStatus", msp.ErrorCode + msp.ErrorDescription );
                }
            }
        }

        #endregion

        #region Properties

        public override bool AllowGuestNavigation
        {
            get
            {
                return true;
            }
        }

        #endregion
    }
}